package com.example.springboottestgit.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.springboottestgit.bean.InstructData;
import com.example.springboottestgit.bean.ResultMsg;
import com.example.springboottestgit.bean.dto.LawDTO;
import com.example.springboottestgit.bean.vo.LawVO;
import com.example.springboottestgit.service.FileService;
import com.example.springboottestgit.service.InviteTenderService;
import com.example.springboottestgit.service.TestService;
import com.example.springboottestgit.service.instruction.CountService;
import com.example.springboottestgit.service.instruction.QualificationReview;
import com.example.springboottestgit.service.instruction.Stage2;
import com.example.springboottestgit.service.instruction.stage1.*;
import com.example.springboottestgit.utils.ExcelExportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/4/12 21:08
 */
@RestController
public class TestController {

    @Autowired
    private TestService service;

    @Autowired
    private CountService countService;
    @Autowired
    private MarginService marginService;
    @Autowired
    private TbExpireService tenderExpireService;
    @Autowired
    private ContractTimeLimit contractTimeLimit;
    @Autowired
    private ZbQueryPeriod tenderQueryPeriod;
    @Autowired
    private TbNoticeTerm tbNoticeTerm;
    @Autowired
    private AddPurchaseAmount addPurchaseAmount;

    @Autowired
    private InviteTenderService tenderService;

    @Autowired
    private tenderBackMargin tenderBackMargin;

    @Autowired
    private FileService fileService;

    @Autowired
    private InviteTendersChangeNotice inviteTendersChangeNotice;

    @Autowired
    private QualificationReview qualificationReview;

    @Autowired
    private PromiseMargin promiseMargin;
    @Autowired
    private Stage2 stage2;

    @Autowired
    private InviteTenderService inviteTenderService;


    @GetMapping("/test")
    public String test(@RequestBody List<InstructData> list) {
        for (InstructData demo : list) {
            demo.setInput("" + demo.getInput());
            demo.setOutput("" + demo.getOutput());
        }
        /*for (InstructData instructData : list) {
            inviteTenderService.askLawQuestion(instructData.getInstruction());
        }*/

        return JSONObject.toJSONString(list);
    }

    @GetMapping("/exe")
    public void test(String exe) throws IOException, InterruptedException {
        fileService.exe(exe);
    }


    @RequestMapping("/law")
    public ResultMsg<List<LawVO>> law(@RequestBody LawDTO dto) {
        List<LawVO> list = new ArrayList<>();
        return ResultMsg.success(list);
    }

    @RequestMapping(path = "/law/excel")
    public void export(@RequestBody LawDTO dto, HttpServletResponse response) {

        List<LawVO> result = new ArrayList<>();

        List<Map<String, Object>> list = JSONObject.parseObject(JSONObject.toJSONString(result), List.class);

        //表头数组
        String[] lables = new String[]{"标题", "涉及内容", "审核结果", "回答", "输出"};
        //查询数据对应的属性数组
        String[] fields = new String[]{"name", "input", "result", "answer", "output"};
        String title = "test.xlsx";
        String headTitle = "招标相关法律法规审核";
        //if (result.size() > 1) {
        //title = result.get(0).getName() + "(" + result.get(0).getCode() + ")" + "历史数据.xlsx";
        //headTitle = result.get(0).getName() + "(" + result.get(0).getCode() + ")" + "历史数据";
        //}
        ExcelExportUtil.titleExport(response, list, lables, fields, title, headTitle);
        //ExcelExportUtil.export(response, list, lables, fields, title);
        //return headTitle;

    }


    @GetMapping("/instruct")
    public List<InstructData> getInstructData() {

        List<InstructData> list = new ArrayList<>();

        //阶段一
        //追加采购金额
        list.addAll(addPurchaseAmount.getInstructData());
        //合同签订时限
        list.addAll(contractTimeLimit.getInstructData());
        //投标有效期
        list.addAll(tenderExpireService.getInstructData());
        //招标公告期限
        list.addAll(tbNoticeTerm.getInstructData());
        //招标文件的质疑时效期
        list.addAll(tenderQueryPeriod.getInstructData());
        //中标 未中标 撤标   退回保证金
        list.addAll(tenderBackMargin.getInstructData());

        //招标文件变更通知
        list.addAll(inviteTendersChangeNotice.getInstructData());
        //提交履约保证金
        list.addAll(promiseMargin.getInstructData());


        //阶段二
        //资格审查
        list.addAll(qualificationReview.getInstructData());

        list.addAll(stage2.getInstructData());

        Set<String> set = new TreeSet<>();


        for (InstructData instructData : list) {
            String s = (String) instructData.getOutput();
            String result = s.substring(0, s.indexOf("。"));
            set.add(instructData.getInstruction() + "#" + result);
        }
        //System.out.println(list.size());
        //System.out.println(set.size());

        for (String s : set) {
            System.out.println(s);
        }


        //String result = JSONObject.toJSONString(list);
        //System.out.println(result.length());
        //System.out.println(result.length() * 3 / 1024 / 1024);
        return list;
    }

    @RequestMapping("/query")
    public Object query(@RequestParam("id") Integer id) {

        return service.query(id);
    }


    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            System.out.println("fsdf");
            HttpUtil.get("http://localhost:8002/test/create/order");
            System.out.println(i);
        }
    }
}
